<?php
namespace app\bis\controller;
use think\Controller;

class Location extends Base
{
    public function index()
    {
     $account = session('bisAccount', '', 'bis');
				//var_dump($account);
				if($account && $account->bis_id){
					 $bis_id = $account->bis_id;
					// return $bis_id;
					}else{				
				    return $this->redirect('login/index');
					}
     
	 $locations = model('BisLocation')->getLocationsByBisId($bis_id);
	 
	 //halt($locations);
	 
	 return $this->fetch('',[
	    'locations' => $locations,
	 ]);  
	}


    public function add()
    {
       if(request()->isPost()){
		   $bisId = $this->getLoginUser()->bis_id;
		   $data = input('post.');
		   //门店基本信息校验

		  
		  
		  //获取经纬度
		 $lnglat = \Map::getLngLat($data['address']);
		 // print_r($lnglat);
		 if(empty($lnglat) || $lnglat["status"] != 0 || $lnglat['result']['precise'] != 1){
			 $this->error('无法获取数据，或者匹配不精准');
			 }
		  
		  
		  //获取二级分类
		  $data['cat'] = '';
		  if(!empty($data['se_category_id'])){
			 // $data['cat'] = implode('|', $data['se_category_id']);
			  }   
		  //门店基本信息入库
		 $locationData = [
		     'bis_id' => $bisId,
			 'name' => $data['name'],
			 'logo' => $data['logo'],
			 'tel' => $data['tel'],
			 'contact' => $data['contact'],
		     'category_id' => $data['category_id'],
			 'category_path' => $data['category_id'].','.$data['cat'],
			 'city_id' => $data['city_id'],
			 'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'],	
			 'api_address' => $data['address'],
			 'open_time' => $data['open_time'],
			 'content' => empty($data['content']) ? '' : $data['content'],
			 'is_main' => 0,
			 'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
			 'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
		 ];
		 if(isset($data['id']) && $data['id'] != ''){
			 $locationId = model('BisLocation')->updateById($locationData, $data['id']);
		  // echo $locationId; exit; 
		   
		  if($locationId){
			  return $this->success('门店编辑成功', url('location/index'));
			  }else{
				  return $this->error('门店编辑失败');
				  }
			 
			 }else{
		 $locationId = model('BisLocation')->add($locationData);
		  // echo $locationId; exit; 
		   
		  if($locationId){
			  return $this->success('门店申请成功', url('location/index'));
			  }else{
				  return $this->error('门店申请失败');
				  }
		  
			 }
		  
		  
		  
		  
		   }else{
			   	 
	 
	 //获取一级城市数据
		$citys = model('City')->getNormalCitysByParentId();
		
		//获取一级分类数据
		$categorys = model('Category')->getNormalCategorysByParentId();
		
		return $this->fetch('',[
		    'citys' => $citys,
			'categorys' => $categorys,
		]);	
	 }
	
	}


 public function status(){
	     $data = input('get.');
		 $res = model('BisLocation')->save(['status'=>$data['status']], 
		     ['id'=>$data['id']]);
	    //halt($res);
		 if($res){
			 $this->success('状态更新成功');
			 }else{
				$this->error('状态更新失败');
				 }
	 
	 }

 public function edit()
    {
     $id = input('get.');
     
	 $location = model('BisLocation')->get($id);
	 
	 //halt($location);
	 //获取一级城市数据
		$citys = model('City')->getNormalCitysByParentId();
		
		$secitys = model('City')->getNormalCitysByParentId($location['city_id']);
		
		$se_city_id = empty($location['city_path'])? '' : explode(',', $location['city_path']);
		
		//获取一级分类数据
		$categorys = model('Category')->getNormalCategorysByParentId();
		
		$secategorys = model('Category')->getNormalCategorysByParentId($location['category_id']);
		
		$secats = empty($location['category_path'])? '' : explode(',', $location['category_path']);
	 
	  // dump($se_city_id);dump($secats);exit;
	 
	 
	 return $this->fetch('',[
	    'location' => $location,
		'citys' => $citys,
		'categorys' => $categorys,
		'secitys' => $secitys,
		'secategorys' => $secategorys,
		'se_city_id' => $se_city_id,
		'secats' => $secats,
		
	 ]);  
	}







}
